package org.lalib.controller.action;

import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CTableForm;
import org.lalib.controller.service.CTableService;
import org.lalib.entity.object.ETable;
import org.lalib.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cTableAction")
public class CTableAction implements IControllerAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private CTableForm cTableForm;

	@Autowired
	private CTableService cTableService;

	@Autowired
	private MessageUtil messageUtil;

	public String init() {
		cTableForm.setTables(getAllTables());
		return "tableDefination";
	}

	public void save() {
		String name = cTableForm.getName();
		Date since = cTableForm.getSince();
		Date till = cTableForm.getTill();

		ETable table = new ETable();
		table.setName(name);
		table.setSince(since);
		table.setTill(till);

		cTableService.save(name, since, till);
		cTableForm.clear();
		messageUtil.addMessage(FacesMessage.SEVERITY_INFO, "Kaydedildi.", "");
		cTableForm.setTables(getAllTables());
	}
	
	public void delete(ETable table){
		table = (ETable)cTableService.getTableById(table.getId());
		cTableService.delete(table);
		cTableForm.setTables(getAllTables());
	}

	public List<ETable> getAllTables() {
		return cTableService.getAllTables();
	}

	public void setcTableForm(CTableForm cTableForm) {
		this.cTableForm = cTableForm;
	}

	public void setcTableService(CTableService cTableService) {
		this.cTableService = cTableService;
	}

	public void setMessageUtil(MessageUtil messageUtil) {
		this.messageUtil = messageUtil;
	}
}